package cn.stylefeng.guns.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

import static cn.stylefeng.guns.modular.demo.service.SyncService.SIMPLE_DATE_FORMAT1;

public class RebateDateUtil {
    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static DateTimeFormatter formatterTime = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
    public static DateTimeFormatter FORMATTER_1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy");
    public static DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("MM");

    /**
     * 上个月月底
     *
     * @return
     */
    public static Long getLastDayOfMonth() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.of(23, 59, 59)).with(TemporalAdjusters.lastDayOfMonth());
        long epochMilli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return epochMilli;
    }

    public static Long getStartOfDayTimestamp(String date) {
        LocalDate localDate = LocalDate.parse(date, SIMPLE_DATE_FORMAT1);
        LocalDateTime startOfDay = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0));
        return startOfDay.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long getStartOfDayTimestamp(LocalDate date) {
        LocalDateTime localDateTime = LocalDateTime.of(date, LocalTime.of(0, 0, 0));
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long getEndOfDayTimestamp(LocalDate date) {
        LocalDateTime localDateTime = LocalDateTime.of(date, LocalTime.of(23, 59, 59));
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    /**
     * 计算月初开始
     *
     * @return
     */
    public static Long getFirstDayOfMonth(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        LocalDateTime localDateTime = dateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 计算月初开始
     *
     * @return
     */
    public static Long getFirstDayOfMonth(String time) {
        LocalDate parse = LocalDate.parse(time, SIMPLE_DATE_FORMAT1);
        LocalDateTime dateTime = LocalDateTime.of(parse, LocalTime.now());
        LocalDateTime localDateTime = dateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    /**
     * 上个月开始
     *
     * @return
     */
    public static Long getFirstDayOfMonth() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.of(0, 0, 0)).with(TemporalAdjusters.firstDayOfMonth());
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 去年结束
     *
     * @return
     */
    public static Long getLastDayOfYeah() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusYears(1), LocalTime.of(23, 59, 59)).with(TemporalAdjusters.lastDayOfYear());
        long epochMilli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return epochMilli;
    }

    /**
     * 去年开始
     *
     * @return
     */
    public static Long getFirstDayOfYeah() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusYears(1), LocalTime.of(0, 0, 0)).with(TemporalAdjusters.firstDayOfYear());
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    /**
     * 昨天 结束
     *
     * @return
     */
    public static Long getLastDayEnd(int day) {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusDays(day), LocalTime.of(23, 59, 59));
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 昨天 开始
     *
     * @return
     */
    public static Long getLastDayBegin(int day) {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusDays(day), LocalTime.of(0, 0, 0));
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    /**
     * 季度月底
     *
     * @return
     */
    public static Long getLastDayOfSeason() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusMonths(3), LocalTime.of(23, 59, 59)).with(TemporalAdjusters.lastDayOfMonth());
        long epochMilli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return epochMilli;
    }

    public static void main(String[] args) {
        Long startOfDayTimestamp = getStartOfDayTimestamp("20240101");
        System.out.println(startOfDayTimestamp);
    }

    public static String format(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        return dateTime.format(formatter);
    }

    public static String formatTime(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        return dateTime.format(formatterTime);
    }

    public static String format1(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());

        return dateTime.format(FORMATTER_1);
    }

    public static String formatYear(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        return dateTime.format(YEAR_FORMATTER);
    }

    public static String formatMonth(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        return dateTime.minusMonths(1).format(MONTH_FORMATTER);
    }

    public static String formatMonth(Long time, int offset) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        return dateTime.minusMonths(offset).format(MONTH_FORMATTER);
    }

    /**
     * 季度开始
     *
     * @return
     */
    public static Long getFirstDayOfSeason() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusMonths(3), LocalTime.of(0, 0, 0)).with(TemporalAdjusters.firstDayOfMonth());
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


}
